package com.njit.springboot.dao.impl;

import com.mysql.jdbc.StringUtils;
import com.njit.springboot.bean.RoncooUser;
import com.njit.springboot.dao.RoncooUserDao;
import com.njit.springboot.util.base.JdbcDaoImpl;
import com.njit.springboot.util.base.Page;
import com.njit.springboot.util.base.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;


@Repository
public class RoncooUserDaoImpl extends JdbcDaoImpl implements RoncooUserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int insert(RoncooUser roncooUser) {
        String sql = "insert into roncoo_user (name, create_time) values (?, ?)";
        return jdbcTemplate.update(sql, roncooUser.getName(),
                roncooUser.getCreateTime());
    }

    @Override
    public int deleteById(int id) {
        String sql = "delete from roncoo_user where id=?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public int updateById(RoncooUser roncooUser) {
        String sql = "update roncoo_user set name=?, create_time=? where id=?";
        return jdbcTemplate.update(sql, roncooUser.getName(),
                roncooUser.getCreateTime(), roncooUser.getId());
    }

    @Override
    public RoncooUser selectById(int id) {
        String sql = "select * from roncoo_user where id=?";
        return queryForObject(sql, RoncooUser.class, id);
    }

    @Override
    public Page<RoncooUser> queryForPage(int pageCurrent, int pageSize, String name) {
        // 若要like查询，如下
        StringBuffer sql = new StringBuffer("select * from roncoo_user where 1");
        if (!StringUtils.isNullOrEmpty(name)) {
// Sql.checkSql 的作用是防止sql注入
            sql.append(" and name like '%").append(Sql.checkSql(name)).append("%' ");
        }
        return queryForPage(sql.toString(), pageCurrent, pageSize, RoncooUser.class);
    }
}

